package sk.vasicek.drinkagent.domain;

import java.util.ArrayList;

import sk.vasicek.drinkagent.R;
import sk.vasicek.drinkagent.service.DrinkPlaceHelper;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class DrinkItemizedOverlay extends ItemizedOverlay {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private DrinkPlaceHelper eventDatabaseHelper;
	private DrinkPoint eventPoint;
	private TextView name;
	private TextView description;
	private TextView ratingLabel;
	private RatingBar eventRating;
	private Button updateButton;
	private Button removeButton;

	public DrinkItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public DrinkItemizedOverlay(Drawable defaultMarker, Context context) {
		this(defaultMarker);
		mContext = context;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		// AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		// dialog.setTitle(item.getTitle());
		// dialog.setMessage(item.getSnippet());
		// dialog.show();
		// Intent intent = new Intent(mContext, DrinkMapDetailActivity.class);
		//
		// if (item.getTitle() != null && !"".equals(item.getTitle())) {
		// intent.putExtra(DrinkPoint.POINT_TITLE, item.getTitle());
		// }
		// if (item.getSnippet() != null && !"".equals(item.getSnippet())) {
		// intent.putExtra(DrinkPoint.POINT_SNIPPET, item.getSnippet());
		// }
		// mContext.startActivity(intent);
		final Dialog mainDialog = new Dialog(mContext,R.style.DrinkDialogTheme);

		mainDialog.setContentView(R.layout.drink_detail);
		mainDialog.setTitle(mContext.getResources().getText(R.string.updatePlaceInfo));
		mainDialog.setCancelable(true);

		eventDatabaseHelper = new DrinkPlaceHelper(mContext);

		if (item.getTitle() != null && !"".equals(item.getTitle())) {
			eventPoint = eventDatabaseHelper.getDrinkMapPointByID(Integer.parseInt(item.getTitle()));
		}

		name = (TextView) mainDialog.findViewById(R.id.title);
		description = (TextView) mainDialog.findViewById(R.id.snippet);
		ratingLabel = (TextView) mainDialog.findViewById(R.id.ratingLabel);
		eventRating = (RatingBar) mainDialog.findViewById(R.id.placeRating);
		if (eventPoint != null) {
			name.setText(eventPoint.getTitle());
			description.setText(eventPoint.getSnippet());
			eventRating.setRating((float) eventPoint.getRating());
		}
		updateButton = (Button) mainDialog.findViewById(R.id.updateButton);
		removeButton = (Button) mainDialog.findViewById(R.id.removeButton);

		updateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean dateOk = true;
				if (eventPoint != null) {
					String name = DrinkItemizedOverlay.this.name.getText().toString();
					if (dateOk) {
						String description = DrinkItemizedOverlay.this.description.getText().toString();
						double rating = (double) eventRating.getRating();
						eventDatabaseHelper.updateDrinkMapPoint(eventPoint.getId(), name + "", description + "", rating);
						Toast.makeText(mContext, mContext.getResources().getText(R.string.placeInfoUpdatedToast), Toast.LENGTH_SHORT).show();
					}
				}
				if (dateOk)
					mainDialog.dismiss();
			}
		});

		removeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
				builder.setMessage(mContext.getResources().getText(R.string.placeRemovePrompt)).setCancelable(true)
						.setPositiveButton(mContext.getResources().getText(R.string.yesAnswer), new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								if (eventPoint != null) {
									eventDatabaseHelper.removeDrinkMapPoint(eventPoint);
									Toast.makeText(mContext, mContext.getResources().getText(R.string.placeRemoveToast), Toast.LENGTH_SHORT).show();
									dialog.dismiss();
									mainDialog.dismiss();
								}
							}
						}).setNegativeButton(mContext.getResources().getText(R.string.noAnswer), new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
				AlertDialog alert = builder.create();
				alert.show();

			}
		});

		mainDialog.show();
		return true;
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}
}
